<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2023/6/14
 * Time: 9:09
 */
namespace app\index\controller;

use think\facade\Db;

class Job{


    /**
     * 获取城市
     * @return string
     */
    public function city(){
        $url = "https://api.1217xc.com/exam2Weixin/getAllCityList";
        $param = [];
        $result = http_post_json($url,$param);
        $result = json_decode($result,true);
        //halt($result['data']);
        $temp = [];
        foreach ($result['data'] as $k=>$v){
            $temp[$k]['region_id'] = $v['region_id'];
            $temp[$k]['region_first_letter'] = $v['region_first_letter'];
            $temp[$k]['region_name'] = $v['region_name'];
        }
        Db::name('region')->insertAll($temp);
        return '同步成功';
    }


    /**
     * 同步章节
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function chapter(){
        set_time_limit(3600);
        ini_set('memory_limit','2024M');
        $city = Db::name('region')
            ->select()->toArray();
        foreach ($city as $k=>$v){
            $url = "https://api.1217xc.com/exam2Weixin/getChapterList";
            $param['token'] = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIxMjE3eGMuY29tIiwiYXVkIjoiMTIxN3hjLmNvbSIsImlhdCI6MTY4NjEyMzU3OSwibmJmIjoxNjg2MTIzNTc5LCJ1c2VyIjp7ImlkXyI6IjE0NDUzIiwidXNlcl9uYW1lIjoiXHU1ZmFlXHU0ZmUxXHU3NTI4XHU2MjM3IiwidXNlcl9waG9uZSI6IjE4ODUyMTIyNTI5IiwidXNlcl9hdmF0YXIiOiJodHRwczpcL1wvdGhpcmR3eC5xbG9nby5jblwvbW1vcGVuXC92aV8zMlwvUE9nRXdoNG1JSE80bmliSDBLbE1FQ05qakd4UVVxMjRaRWFHVDRwb0M2aWNSaWNjVkdLU3lYd2liY1BxNEJXbWlhSUd1RzFpY3d4YVFYNmdyQzlWZW1ab0o4cmdcLzEzMiIsInd4dXNlcl9pZCI6IjE2MDgwIiwid3h1c2VyX3VuaW9uaWQiOiJvQTB1UnM3OFhiWDl2clZEdm5taFVNdTFCcjhBIiwidHR1c2VyX2lkIjpudWxsLCJ0dHVzZXJfdW5pb25pZCI6bnVsbCwicm9sZV8iOiIwIiwibG9jYXRpb25fIjoiXHU2NzZkXHU1ZGRlIiwic3RhdHVzXyI6IjEiLCJhZGRfdGltZSI6IjIwMjMtMDYtMDcgMTU6Mzk6MzciLCJsb25naXR1ZGVfIjpudWxsLCJsYXRpdHVkZV8iOm51bGx9fQ.swqbifz48Is_FEXvPqJ3x5W2X2HMBIRdteuOQssBmK0';
            $param['city'] = $v['region_name'];
            $result = http_post_json($url,$param);
            $result = json_decode($result,true);
            $temp = [];
            foreach ($result['data'] as $kk=>$vv){
                $temp[$kk]['id_'] = $vv['id_'];
                $temp[$kk]['cid_'] = $vv['cid_'];
                $temp[$kk]['chaper_'] = $vv['chaper_'];
                $temp[$kk]['total_'] = $vv['total_'];
                $temp[$kk]['judge_num'] = $vv['judge_num'];
                $temp[$kk]['single_sel_num'] = $vv['single_sel_num'];
                $temp[$kk]['muti_sel_num'] = $vv['muti_sel_num'];
                $temp[$kk]['fill_blank_num'] = $vv['fill_blank_num'];
                $temp[$kk]['pid_'] = $vv['pid_'];
                $temp[$kk]['city_'] = $vv['city_'];
                $temp[$kk]['update_at'] = currentTime();
                $temp[$kk]['create_at'] = currentTime();
            }
            Db::name('chapter')->insertAll($temp);
        }
        halt('同步章节成功');
    }


    /**
     * 同步技巧试卷
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function skillPaper(){
        set_time_limit(3600);
        ini_set('memory_limit','2024M');
        $city = Db::name('region')
            ->select()->toArray();
        $temp = [];
        foreach ($city as $k=>$v){
            $url = "https://api.1217xc.com/exam2Weixin/getSkillList";
            $param['token'] = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIxMjE3eGMuY29tIiwiYXVkIjoiMTIxN3hjLmNvbSIsImlhdCI6MTY4NjEyMzU3OSwibmJmIjoxNjg2MTIzNTc5LCJ1c2VyIjp7ImlkXyI6IjE0NDUzIiwidXNlcl9uYW1lIjoiXHU1ZmFlXHU0ZmUxXHU3NTI4XHU2MjM3IiwidXNlcl9waG9uZSI6IjE4ODUyMTIyNTI5IiwidXNlcl9hdmF0YXIiOiJodHRwczpcL1wvdGhpcmR3eC5xbG9nby5jblwvbW1vcGVuXC92aV8zMlwvUE9nRXdoNG1JSE80bmliSDBLbE1FQ05qakd4UVVxMjRaRWFHVDRwb0M2aWNSaWNjVkdLU3lYd2liY1BxNEJXbWlhSUd1RzFpY3d4YVFYNmdyQzlWZW1ab0o4cmdcLzEzMiIsInd4dXNlcl9pZCI6IjE2MDgwIiwid3h1c2VyX3VuaW9uaWQiOiJvQTB1UnM3OFhiWDl2clZEdm5taFVNdTFCcjhBIiwidHR1c2VyX2lkIjpudWxsLCJ0dHVzZXJfdW5pb25pZCI6bnVsbCwicm9sZV8iOiIwIiwibG9jYXRpb25fIjoiXHU2NzZkXHU1ZGRlIiwic3RhdHVzXyI6IjEiLCJhZGRfdGltZSI6IjIwMjMtMDYtMDcgMTU6Mzk6MzciLCJsb25naXR1ZGVfIjpudWxsLCJsYXRpdHVkZV8iOm51bGx9fQ.swqbifz48Is_FEXvPqJ3x5W2X2HMBIRdteuOQssBmK0';
            $param['city'] = $v['region_name'];
            $result = http_post_json($url,$param);
            $result = json_decode($result,true);
            foreach ($result['data'] as $kk=>$vv){
                if($vv['city_'] !== '0'){
                    $temp[] = [
                        'id_'=>$vv['id_'],
                        'city_'=>$vv['city_'],
                        'title_'=>$vv['title_'],
                        'sub_title'=>$vv['sub_title'],
                    ];
                }
            }
        }
        Db::name('skill_paper')->insertAll($temp);
    }


    /**
     * 技巧章节
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function skillChapter(){
        set_time_limit(3600);
        ini_set('memory_limit','2024M');
        $paper = Db::name('skill_paper')
            ->select()->toArray();
        $temp = [];
        foreach ($paper as $k=>$v){
            $url = "https://api.1217xc.com/exam2Weixin/getSkillDetail";
            $param['token'] = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIxMjE3eGMuY29tIiwiYXVkIjoiMTIxN3hjLmNvbSIsImlhdCI6MTY4NjEyMzU3OSwibmJmIjoxNjg2MTIzNTc5LCJ1c2VyIjp7ImlkXyI6IjE0NDUzIiwidXNlcl9uYW1lIjoiXHU1ZmFlXHU0ZmUxXHU3NTI4XHU2MjM3IiwidXNlcl9waG9uZSI6IjE4ODUyMTIyNTI5IiwidXNlcl9hdmF0YXIiOiJodHRwczpcL1wvdGhpcmR3eC5xbG9nby5jblwvbW1vcGVuXC92aV8zMlwvUE9nRXdoNG1JSE80bmliSDBLbE1FQ05qakd4UVVxMjRaRWFHVDRwb0M2aWNSaWNjVkdLU3lYd2liY1BxNEJXbWlhSUd1RzFpY3d4YVFYNmdyQzlWZW1ab0o4cmdcLzEzMiIsInd4dXNlcl9pZCI6IjE2MDgwIiwid3h1c2VyX3VuaW9uaWQiOiJvQTB1UnM3OFhiWDl2clZEdm5taFVNdTFCcjhBIiwidHR1c2VyX2lkIjpudWxsLCJ0dHVzZXJfdW5pb25pZCI6bnVsbCwicm9sZV8iOiIwIiwibG9jYXRpb25fIjoiXHU2NzZkXHU1ZGRlIiwic3RhdHVzXyI6IjEiLCJhZGRfdGltZSI6IjIwMjMtMDYtMDcgMTU6Mzk6MzciLCJsb25naXR1ZGVfIjpudWxsLCJsYXRpdHVkZV8iOm51bGx9fQ.swqbifz48Is_FEXvPqJ3x5W2X2HMBIRdteuOQssBmK0';
            $param['cid'] = $v['id_'];
            $result = http_post_json($url,$param);
            $result = json_decode($result,true);
            foreach ($result['data']['skilllist'] as $kk=>$vv){
                $temp[] = [
                    'id_'=>$vv['id_'],
                    'title_'=>$vv['title_'],
                    'content_'=>$vv['content_'],
                    'type_'=>$vv['type_'],
                    'city_'=>$v['city_'],
                    'skill_paper_id'=>$v['id_'],
                ];
            }
        }
        Db::name('skill_chapter')->insertAll($temp);
    }




    public function test(){
        set_time_limit(3600);
        ini_set('memory_limit','2024M');
        $region = Db::name('region')
            ->select()->toArray();
        foreach ($region as $k=>$v){
            //获取
            $city = Db::name('province_city_info')
                ->where('name',$v['region_name'])
                ->find();
            if(!empty($city)){
                //则更新全称
                Db::name('region')
                    ->where('region_id',$v['region_id'])
                    ->update(['region_full_name'=>$city['fullname']]);
            }
        }

        halt('执行完成');
    }
}